package com.huichaoliu.stylist.activity;

import java.util.ArrayList;
import java.util.List;

import com.huichaoliu.app.R;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class StylistMainActivity extends FragmentActivity implements
		OnClickListener {
	// Views
	TextView back;
	TextView title;
	TextView pic_add;

	RelativeLayout r_shop;
	RelativeLayout r_pic;
	RelativeLayout r_sos;
	RelativeLayout r_mine;

	ViewPager viewPage;

	// tools
	List<Fragment> listFragment;

	// Fragment
	StylistShoppingFragment fragmentShopping;
	StylistShowPicFragment fragmentShowPic;
	StylistSosFragment fragmentSos;
	StylistMineFragment fragmentMine;

	@SuppressLint("InlinedApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_stylist_main);

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}

		initTools();
		initView();
	}

	private void initTools() {
		listFragment = new ArrayList<Fragment>();

		fragmentShopping = new StylistShoppingFragment();
		fragmentShowPic = new StylistShowPicFragment() {

			@Override
			public void showOrHide(boolean bool) {
				if (bool) {
					pic_add.setVisibility(View.VISIBLE);
				} else {
					pic_add.setVisibility(View.GONE);
				}
			}

		};
		fragmentSos = new StylistSosFragment();
		fragmentMine = new StylistMineFragment();

		listFragment.add(fragmentShopping);
		listFragment.add(fragmentShowPic);
		listFragment.add(fragmentSos);
		listFragment.add(fragmentMine);
	}

	private void initView() {
		back = (TextView) findViewById(R.id.stylist_back);
		title = (TextView) findViewById(R.id.stylist_name);
		pic_add = (TextView) findViewById(R.id.stylist_show_pic_add);
		pic_add.setVisibility(View.GONE);

		viewPage = (ViewPager) findViewById(R.id.viewPager);

		r_shop = (RelativeLayout) findViewById(R.id.layout_stylist_shop);
		r_sos = (RelativeLayout) findViewById(R.id.layout_stylist_sos);
		r_pic = (RelativeLayout) findViewById(R.id.layout_stylist_pic);
		r_mine = (RelativeLayout) findViewById(R.id.layout_stylist_mine);

		choosBtn(1);

		back.setOnClickListener(this);
		r_shop.setOnClickListener(this);
		r_sos.setOnClickListener(this);
		r_pic.setOnClickListener(this);
		r_mine.setOnClickListener(this);
		pic_add.setOnClickListener(fragmentShowPic);

		viewPage.setAdapter(new MyFragmentPagerAdapter(
				getSupportFragmentManager(), listFragment));
		viewPage.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				choosBtn(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		viewPage.setCurrentItem(1);
	}

	public void choosBtn(int code) {
		r_shop.setBackgroundColor(getResources().getColor(R.color.tras));
		r_sos.setBackgroundColor(getResources().getColor(R.color.tras));
		r_pic.setBackgroundColor(getResources().getColor(R.color.tras));
		r_mine.setBackgroundColor(getResources().getColor(R.color.tras));
		switch (code) {
		case 0:
			r_shop.setBackgroundResource(R.drawable.stylist_btn_p);
			title.setText("逛街");
			pic_add.setVisibility(View.GONE);
			break;
		case 1:
			r_pic.setBackgroundResource(R.drawable.stylist_btn_p);
			title.setText("晒图");
			if (fragmentShowPic.isShowAdd()) {
				pic_add.setVisibility(View.VISIBLE);
			} else {
				pic_add.setVisibility(View.GONE);
			}
			break;
		case 2:
			r_sos.setBackgroundResource(R.drawable.stylist_btn_p);
			title.setText("搭救");
			pic_add.setVisibility(View.GONE);
			break;
		case 3:
			r_mine.setBackgroundResource(R.drawable.stylist_btn_p);
			title.setText("我的");
			pic_add.setVisibility(View.GONE);
			break;
		default:
			break;
		}
	}

	public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
		private List<Fragment> fragmentsList = new ArrayList<Fragment>();

		public MyFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		public MyFragmentPagerAdapter(FragmentManager fragmentManager,
				List<Fragment> fragments) {
			super(fragmentManager);
			this.fragmentsList = fragments;
		}

		@Override
		public int getCount() {
			return fragmentsList.size();
		}

		@Override
		public Fragment getItem(int arg0) {
			return fragmentsList.get(arg0);
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

	}

	@Override
	public void onClick(View arg0) {
		if (fragmentShopping != null) {
			fragmentShopping.dissmissAll();
		}
		switch (arg0.getId()) {
		case R.id.stylist_back:
			finish();
			break;
		case R.id.layout_stylist_shop:
			choosBtn(0);
			viewPage.setCurrentItem(0);
			break;
		case R.id.layout_stylist_pic:
			choosBtn(1);
			viewPage.setCurrentItem(1);
			break;
		case R.id.layout_stylist_sos:
			choosBtn(2);
			viewPage.setCurrentItem(2);
			break;
		case R.id.layout_stylist_mine:
			choosBtn(3);
			viewPage.setCurrentItem(3);
			break;
		default:
			break;
		}
	}

}
